﻿package com.mttang.loader
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	import flash.system.Security;
	import flash.system.LoaderContext;
	
	public class SwfLoader extends EventDispatcher
	{
		private static var All_Swfloader_List:Array ;
		public  var url:String = "";
		public  var countIndex:uint =0;
		
		private var _loaderArr:Array=[];
		private var _loadUrlArr:Array = [];

		private var comCount:int;
		private var _comFlag:Boolean;
		private var lc:LoaderContext = new LoaderContext(true);
		
		public function SwfLoader()
		{
			if (!All_Swfloader_List) All_Swfloader_List = [];
			All_Swfloader_List.push(this);
			
		}
		/**
		 * 地址数组
		 * @param	urlArr
		 */
		public function load(urlArr:Array):void
		{
			Security.allowDomain("*");
			if (urlArr.length == 0) {
				_comFlag = true;
				this.dispatchEvent(new Event(Event.COMPLETE));
				return;
			}
			
			comCount = 0;
			for (var j:int = 0; j < urlArr.length; j++)
			{
				if (_loadUrlArr.indexOf(urlArr[j]) != -1) continue;
				_loadUrlArr.push( url+ urlArr[j]);
			}
			_loaderArr = [];
			for(var i:int=0;i<_loadUrlArr.length;i++)
			{
				var loader:Loader=new Loader;
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				loader.load(new URLRequest(_loadUrlArr[i]),lc);
				
				_loaderArr.push(loader);
			}
			
		}
		/**
		 * 加载单个
		 * @param	url
		 */
		public function loadOne(swfUrl:String):void
		{
			
			comCount = 0;
			_loadUrlArr.push( url+ swfUrl);
			
			var loader:Loader=new Loader;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.load(new URLRequest(_loadUrlArr[0]),lc);
				
			_loaderArr.push(loader);
			
		}
		
		// 获取第一个加载的class
		public function getOneClass(className:String=null):Class
		{
			var tempLoader:Loader = _loaderArr[0];
				
			var cName:String;
			if (className) {
				cName = className;
			}else {
				cName = getChildClassName(tempLoader);
			}
			return getClassByName(tempLoader,cName );
		}
		// 获取第一个加载的MC
		public function getOneMovieClip(className:String=null):MovieClip
		{
			var clas:Class = getOneClass(className);
			if (clas) {
				return new clas;
			}else {
				return null;
			}
		}
		
		
		/**
		 * 获取ui类
		 * @param	swfName	类名
		 * @return	ui class
		 */
		public function getClass(swfName:String, className:String=null):Class
		{
			var index:int=-1;
			for(var i:int=0;i<_loadUrlArr.length;i++)
			{ 
				if(this.url + swfName==_loadUrlArr[i]){
					index=i;
					break;
				}
			}
			
			if (index != -1) {
				
				var tempLoader:Loader = _loaderArr[index];
				
				var cName:String;
				if (className) {
					cName = className;
				}else {
					cName = getChildClassName(tempLoader);
				}
				return getClassByName(tempLoader,cName );
				 
			}else return null;
		}
		/**
		 * 从所有加载器中获取class
		 * @param	swfName	类名
		 * @return	class
		 */
		static public function autoGetClass(swfName:String,className:String=null):Class
		{

				var tempLoader:Loader = autoGetLoader(swfName);
				if(!tempLoader) return null;
				
				var cName:String;
				if (className) {
					cName = className;
				}else {
					cName = getChildClassName(tempLoader);
				}
				return getClassByName(tempLoader,cName );
			
		}
		/**
		 * 获取相应的loader
		 * @param	swfName
		 * @return
		 */
		static public function autoGetLoader(swfName:String):Loader
		{
			var index:int = -1;
			var loaderIndex:int = -1;
			for (var i:int = 0; i < All_Swfloader_List.length; i++)
			{
				var lo:SwfLoader = All_Swfloader_List[i];
				if (!lo.complete) continue;
				for(var j:int=0;j<lo.loadUrlArr.length;j++)
				{ 
					if(lo.url + swfName==lo.loadUrlArr[j]){
						index = j;
						loaderIndex = i;
						break;
					}
				}
			}
			if (index != -1 && loaderIndex != -1 ) {
				lo = All_Swfloader_List[loaderIndex];
				var tempLoader:Loader = lo.loaderArr[index];
				return tempLoader;
			}else return null;
		}
		
		static public function getClassByName(lo:Loader ,cName:String):Class
		{
			Security.allowDomain("*");
			if (lo.content) {
				return lo.contentLoaderInfo.applicationDomain.getDefinition(cName) as Class;
			}else {
				return null
			}
		}
		/**
		 * 获取指定loader位置的mc的类名
		 * @param	lo
		 * @param	childIndex
		 * @return
		 */
		static public function getChildClassName(lo:Loader ,childIndex:int =0 ):String
		{
			var target:* = lo.content ;
			if (!target) return null;
			var child:*= (target as Sprite) .getChildAt(childIndex);
			if(child)
				return getQualifiedClassName (child); 
			else 
				return null
		}
		
		/**
		 * 获取对象
		 * @param	swfName	类名
		 * @param	childIndex
		 * @return
		 */
		public function getMovieClip(swfName:String,childIndex:uint):MovieClip
		{
			var index:int=-1;
			for(var i:int=0;i<_loadUrlArr.length;i++)
			{ 
				if(swfName==_loadUrlArr[i]){
					index=i;
					break;
				}
			}
			if(index!=-1){
				var tempLoader:Loader=_loaderArr[index];
				var targetSwf:MovieClip=tempLoader.contentLoaderInfo.content as MovieClip;
				if (targetSwf&&childIndex < targetSwf.numChildren) {
					return targetSwf.getChildAt(childIndex) as MovieClip;
				}
				else return null;
			}else return null;
		}
		
		
		private function loaderCompleteHandler(e:Event):void
		{ 
			checkComplete();
		}
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			checkComplete();
			trace("urlPath:"+ url+"; url error......"+ e.text);
		}
		private function checkComplete():void
		{
			comCount++;
			if (comCount == _loadUrlArr.length) {
				_comFlag = true;
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		/**
		 * 销毁
		 */
		public function destroy():void
		{
			var index:int = All_Swfloader_List.indexOf(this);
			if (index >= 0 ) All_Swfloader_List.splice(index, 1);
			
			for(var i:int=0;i<_loaderArr.length;i++)
			{
				var loader:Loader=_loaderArr[i];
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
				loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				loader.unload();
				loader=null;
			}
			_loaderArr = null;
			_loadUrlArr=null;
		}
		/**
		 * 获取json 数据中地址数据
		 * @param	dataArr
		 * @return
		 */
		public function getUrlList(dataArr:Array):Array
		{
			var arr:Array = [] ;
			
			for(var i:int=0;i<dataArr.length;i++)
			{
				var objUtil:Object = dataArr[i];
				
				arr.push( objUtil.src);
			}
			return arr;
		}
		
		public function get loadUrlArr():Array
		{
			return _loadUrlArr;
		}
		
		public function get loaderArr():Array
		{
			return _loaderArr;
		}
		public function get complete():Boolean
		{
			return _comFlag;
		}
	}
}